package com.gmics.unifiq.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQLController {
	private Connection con;

	/***********************************************************
	 * Method Name : getColumnNames Purpose : Obtain the column names of the
	 * result set Return : Array of String
	 **********************************************************/
	public String[] getColumnNames(ResultSet rs) {
		String[] columnNames = null;
		try {
			// Get result set meta data
			ResultSetMetaData rsmd = rs.getMetaData();
			columnNames = new String[rsmd.getColumnCount()];

			// Get the column names; column indices start from 1
			for (int i = 0; i < columnNames.length; i++) {
				columnNames[i] = rsmd.getColumnName(i + 1);
			}
		} catch (SQLException e) {
			System.out.println("Error in getColumnNames() in DBController");
			e.printStackTrace();
		}
		return columnNames;
	}

	public void getConnection() {
		String url = "";
		try {
			url = "jdbc:mysql://unifiqinstance.cmhjlldecsyp.us-west-2.rds.amazonaws.com:3306/UnifiqDatabase?user=unifiq_admin&password=unifiq_password";
			con = DriverManager.getConnection(url, "unifiq_admin",
					"unifiq_password");
		} catch (java.sql.SQLException e) {
			System.out.println("Connection failed ->" + url);
		}
	}

	/************************************************************
	 * Method Name : readRequest Input Parameter : String (database query)
	 * Purpose : Obtain the result set from the db query Return : resultSet
	 * (records from the query)
	 ************************************************************/
	public ResultSet readRequest(String dbQuery) {
		ResultSet rs = null;
		try {
			// create a statement object
			Statement stmt = con.createStatement();
			// execute an SQL query and get the result
			rs = stmt.executeQuery(dbQuery);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	/***********************************************************
	 * Method Name : terminate Input Parameter : nil Purpose : Close db
	 * conection Return :nil
	 **********************************************************/
	public void terminate() {
		// close connection
		try {
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*
	 * public static void main(String[] arg)throws Exception{ MySQLController db
	 * = new MySQLController(); db.testDriver(); db.getConnection();
	 * db.terminate(); }
	 */

	/********************************************************
	 * Method Name : testDriver Input Parameter : nil Purpose : To test if the
	 * driver is properly installed (whether your J-Connector for MySQL is
	 * installed properly) Return :nil
	 *******************************************************/
	public void testDriver() throws Exception {
		System.out.println("Initializing Server... ");
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			System.out.println(" Driver Found.");
		} catch (ClassNotFoundException e) {
			System.out.println(" Driver Not Found, exiting..");
			throw (e);
		}
	}

	/***********************************************************
	 * Method Name : updateRequest Input Parameter : String (database query)
	 * Purpose : Execute update using the db query Return : int (count is 1 if
	 * successful)
	 ***********************************************************/
	public int updateRequest(String dbQuery) {
		int count = 0;
		try {
			// create a statement object
			Statement stmt = con.createStatement();
			// execute an SQL query and get the result
			count = stmt.executeUpdate(dbQuery);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
}